var GroupObj = require('../../sjg/kernel/obj/GroupObj');
var TextObj = require('../../sjg/kernel/obj/TextObj');
var Parser = require('../../sjg/kernel/base/Parser');
var CompoundPath = require('../../sjg/common/graph/CompoundPath');
var PointPath = require('../../sjg/common/graph/PointPath');
var Point = require('../../sjg/common/graph/Point');
var moment = require('moment'); // 日期格式化

var CommonUtil = (function () {
    function CommonUtil() {
    }

    // 'HH_mm_ss_SSS_'+随机数的字符串
    CommonUtil.genRandomStr = function () {
        var m = moment(new Date());
        var newDirName = m.format('HH_mm_ss_SSS_') + Math.floor(Math.random() * 100000);
        return newDirName;
    };

    // 今天偏移i天得到'YYYY/MM/DD'的字符串
    CommonUtil.genDayStr = function (i) {
        if (i === void 0) { i = 0; }
        var m = moment(new Date());
        return moment(m).add(i, 'days').format('YYYY/MM/DD');
    };

    // 深度复制［复制自身和子对象，递归］
    // 参数：obj 内核对象
    // 参数：ver 数据版本
    // 参数：g GlobalData
    CommonUtil.deepCopy = function (obj, ver, g) {
        var jsonStr = obj.toJson().replace(/\n/g, '\\n'); // 处理Text对象的换行符
        var copy = Parser.fromUnknownJson(JSON.parse(jsonStr), ver, g);
        CommonUtil.deepSetId(copy, g);
        return copy;
    };

    // 为对象和子对象设置id，递归
    CommonUtil.deepSetId = function (obj, g) {
        g.objManager().saveObject(obj, true);
        if (obj instanceof GroupObj) {
            var grpObj = obj;
            for (var i in grpObj.children) {
                var childObj = grpObj.children[i];
                CommonUtil.deepSetId(childObj, g);
            }
        }
        // 处理文字的抬头
        if (obj instanceof TextObj) {
            var textObj = obj;
            if (textObj.head) {
                CommonUtil.deepSetId(textObj.head, g);
            }
        }
    };

    CommonUtil.createBasicCompoundPath = function (type) {
        var ret = null;
        var data = CommonUtil.g_data[type];
        ret = new CompoundPath();
        var minx, maxx, miny, maxy;
        for (var i = 0; i < data.length; i++) {
            var path = data[i];
            var path0 = new PointPath();
            for (var j = 0; j < path.length; j++) {
                var point = path[j];
                var pt0 = new Point();
                pt0.x = point[0];
                pt0.y = point[1];
                pt0.type = point[2];
                path0.points.push(pt0);
                if (i == 0 && j == 0) {
                    minx = pt0.x;
                    maxx = pt0.x;
                    miny = pt0.y;
                    maxy = pt0.y;
                }
                else {
                    minx = minx < pt0.x ? minx : pt0.x;
                    maxx = maxx > pt0.x ? maxx : pt0.x;
                    miny = miny < pt0.y ? miny : pt0.y;
                    maxy = maxy > pt0.y ? maxy : pt0.y;
                }
            }
            ret.paths.push(path0);
        }
        ret.w = maxx - minx;
        ret.h = maxy - miny;
        return ret;
    };

    CommonUtil.createCompoundPath = function (type, w, h) {
        var ret = CommonUtil.createBasicCompoundPath(type);
        ret.adjustPosition(w / ret.w, h / ret.h, 2);
        return ret;
    };

    // 创建复合路径
    CommonUtil.COMPOUNDPATH_RECT = 0;
    CommonUtil.COMPOUNDPATH_CIRCLE = 1;
    CommonUtil.COMPOUNDPATH_ROUND_RECT = 2;
    CommonUtil.COMPOUNDPATH_SIX = 3;
    CommonUtil.COMPOUNDPATH_EIGHT = 4;
    // 0矩形
    CommonUtil.g_data = [
        [
            [[0, 0, 8], [50, 0, 1], [50, 50, 1], [0, 50, 1]]
        ],
        [
            [[25, 0, 8], [39, 0, 3], [50, 11, 3], [50, 25, 3], [50, 39, 3], [39, 50, 3], [25, 50, 3], [11, 50, 3], [0, 39, 3], [0, 25, 3], [0, 11, 3], [11, 0, 3], [25, 0, 3]]
        ],
        [
            [[4.48, 0, 8], [45.52, 0, 1], [48, 0, 3], [50, 2, 3], [50, 4.48, 3], [50, 45.52, 1], [50, 48, 3], [48, 50, 3], [45.52, 50, 3], [4.48, 50, 1], [2, 50, 3], [0, 48, 3], [0, 45.52, 3], [0, 4.48, 1], [0, 2, 3], [2, 0, 3], [4.48, 0, 3]]
        ],
        [
            [[12.5, 0, 8], [37.5, 0, 1], [50, 21.65, 1], [37.5, 43.3, 1], [12.5, 43.3, 1], [0, 21.65, 1]]
        ],
        [
            [[14.65, 0, 8], [35.35, 0, 1], [50, 14.64, 1], [50, 35.35, 1], [35.35, 50, 1], [14.65, 50, 1], [0, 35.35, 1], [0, 14.64, 1]]
        ]
    ];

    return CommonUtil;
})();

module.exports = CommonUtil;